package io.adminboot.web;

import io.adminboot.domain.param.JqGridParam;
import io.adminboot.domain.vo.PageVO;
import io.adminboot.entity.ScheduleJob;
import io.adminboot.service.ScheduleJobService;
import io.adminboot.system.annotation.AddSysLog;
import io.adminboot.system.validator.ValidatorUtils;
import io.adminboot.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 定时任务
 */
@RestController
@RequestMapping("/sys/schedule")
public class ScheduleJobController {
    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 定时任务列表
     */
    @GetMapping("/list")
//	@RequiresPermissions("sys:schedule:list")
    public R list(JqGridParam param) {
        String beanName = null;
        Map<String, String> map = param.getKeywords();
        if (map != null && StringUtils.isNotBlank(map.get("beanName"))) {
            beanName = map.get("beanName");
        }

        PageVO page = scheduleJobService.queryPage(beanName, param.getPageNum(), param.getPageSize());

        return R.ok().put("page", page);
    }

    /**
     * 定时任务信息
     */
    @GetMapping("/info/{jobId}")
//	@RequiresPermissions("sys:schedule:info")
    public R info(@PathVariable("jobId") Long jobId) {
        ScheduleJob schedule = scheduleJobService.selectById(jobId);

        return R.ok().put("schedule", schedule);
    }

    /**
     * 保存定时任务
     */
    @AddSysLog("保存定时任务")
    @PostMapping("/save")
//	@RequiresPermissions("sys:schedule:save")
    public R save(@RequestBody ScheduleJob scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);

        scheduleJobService.save(scheduleJob);

        return R.ok();
    }

    /**
     * 修改定时任务
     */
    @AddSysLog("修改定时任务")
    @PostMapping("/update")
//	@RequiresPermissions("sys:schedule:update")
    public R update(@RequestBody ScheduleJob scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);

        scheduleJobService.update(scheduleJob);

        return R.ok();
    }

    /**
     * 删除定时任务
     */
    @AddSysLog("删除定时任务")
    @PostMapping("/delete")
//	@RequiresPermissions("sys:schedule:delete")
    public R delete(@RequestBody Long[] jobIds) {
        scheduleJobService.deleteBatch(jobIds);

        return R.ok();
    }

    /**
     * 立即执行任务
     */
    @AddSysLog("立即执行任务")
    @PostMapping("/run")
//	@RequiresPermissions("sys:schedule:run")
    public R run(@RequestBody Long[] jobIds) {
        scheduleJobService.run(jobIds);

        return R.ok();
    }

    /**
     * 暂停定时任务
     */
    @AddSysLog("暂停定时任务")
    @PostMapping("/pause")
//	@RequiresPermissions("sys:schedule:pause")
    public R pause(@RequestBody Long[] jobIds) {
        scheduleJobService.pause(jobIds);

        return R.ok();
    }

    /**
     * 恢复定时任务
     */
    @AddSysLog("恢复定时任务")
    @PostMapping("/resume")
//	@RequiresPermissions("sys:schedule:resume")
    public R resume(@RequestBody Long[] jobIds) {
        scheduleJobService.resume(jobIds);

        return R.ok();
    }

}
